
* Generate a merge key for ASM energy data

gen naics_asm = naics
tostring naics_asm, replace


*replace naics_asm = "31131" if floor(naics/10) == 31131 
*replace naics_asm = "31181" if floor(naics/10) == 31181 
replace naics_asm = "31131N" if (naics == 311312 | naics == 311311) & year <=2011
replace naics_asm = "31181M" if floor(naics/10) == 31181 & year <=2011

replace naics_asm = "31211M" if floor(naics/10) == 31211 & year <= 2011
replace naics_asm = "31222M" if floor(naics/10) == 31222 & year <= 2011
replace naics_asm = "31311M" if floor(naics/10) == 31311 & year <= 2011
replace naics_asm = "31322M" if floor(naics/10) == 31322 & year <= 2011
replace naics_asm = "31324M" if floor(naics/10) == 31324 & year <= 2011


replace naics_asm = "31331M" if floor(naics/10) == 31331 & year <= 2011
replace naics_asm = "31412M" if floor(naics/10) == 31412 & year <= 2011
replace naics_asm = "31491M" if floor(naics/10) == 31491 & year <= 2011
replace naics_asm = "31511M" if floor(naics/10) == 31511 & year <= 2011
replace naics_asm = "31519M" if floor(naics/10) == 31519 & year <= 2011
replace naics_asm = "31521M" if floor(naics/10) == 31521 & year <= 2011
replace naics_asm = "31599M" if floor(naics/10) == 31599 & year <= 2011
replace naics_asm = "31621M" if floor(naics/10) == 31621 & year <= 2011
replace naics_asm = "31699M" if floor(naics/10) == 31699 & year <= 2011
replace naics_asm = "32111M" if floor(naics/10) == 32111 & year <= 2011


replace naics_asm = "32121N" if naics == 321211 & year <= 2011
replace naics_asm = "32121N" if naics == 321212 & year <= 2011
replace naics_asm = "32121P" if naics == 321213 & year <= 2011
replace naics_asm = "32121P" if naics == 321214 & year <= 2011


replace naics_asm = "32191M" if floor(naics/10) == 32191 & year <= 2011
replace naics_asm = "32221M" if floor(naics/10) == 32221 & year <= 2011
replace naics_asm = "32222N" if (naics == 322221 | naics == 322222 | ///
								naics == 322225 | naics == 322226 ) & year <= 2011
replace naics_asm = "32222P" if (naics == 322223 | naics == 322224 ) & year <= 2011


replace naics_asm = "32223M" if floor(naics/10) == 32223 & year <= 2011
replace naics_asm = "32311M" if floor(naics/10) == 32311 & year <= 2011
replace naics_asm = "32312M" if floor(naics/10) == 32312 & year <= 2011
replace naics_asm = "32561M" if floor(naics/10) == 32561 & year <= 2011


replace naics_asm = "32599N" if (naics == 325992 | naics == 325998) & year <= 2011
replace naics_asm = "32619M" if floor(naics/10) == 32619 & year <= 2011
replace naics_asm = "32621M" if floor(naics/10) == 32621 & year <= 2011
replace naics_asm = "32629M" if floor(naics/10) == 32629 & year <= 2011
replace naics_asm = "32733M" if floor(naics/10) == 32733 & year <= 2011
replace naics_asm = "33131N" if (naics == 331315 | naics == 331319) & year <= 2011
replace naics_asm = "33142M" if floor(naics/10) == 33142 & year <= 2011

replace naics_asm = "33152N" if (naics == 331521 | naics == 331524) & year <= 2011
replace naics_asm = "33152P" if (naics == 331522 | naics == 331525 | ///
								naics == 331528) & year <= 2011
								

replace naics_asm = "33211N" if (naics == 332111 | naics == 332112) & year <= 2011
replace naics_asm = "33211P" if (naics == 332115 | naics == 332116) & year <= 2011
replace naics_asm = "33221N" if (naics == 332211 | naics == 332214) & year <=2011

replace naics_asm = "33221P" if (naics == 332212 | naics == 332213) & year <=2011

replace naics_asm = "33231M" if floor(naics/10) == 33231 & year <= 2011
replace naics_asm = "33232M" if floor(naics/10) == 33232 & year <= 2011
replace naics_asm = "33243M" if floor(naics/10) == 33243 & year <= 2011
replace naics_asm = "33261M" if floor(naics/10) == 33261 & year <= 2011
replace naics_asm = "33281M" if floor(naics/10) == 33281 & year <= 2011

replace naics_asm = "33291N" if naics == 332911 & year <= 2011
replace naics_asm = "33291N" if naics == 332912 & year <= 2011
replace naics_asm = "33291N" if naics == 332919 & year <= 2011

replace naics_asm = "33299N" if naics == 332997 & year <= 2011
replace naics_asm = "33299N" if naics == 332998 & year <= 2011
replace naics_asm = "33299N" if naics == 332999 & year <= 2011

replace naics_asm = "33313M" if floor(naics/10) == 33313 & year <= 2011
replace naics_asm = "33329N" if (naics == 333291 | naics == 333292 | ///
								naics == 333293 | naics == 333294 | ///
								naics == 333298) & year <= 2011
								
replace naics_asm = "33331N" if (naics == 333311 | naics == 333312 | naics == 333319) & year <= 2011
replace naics_asm = "33341N" if (naics == 333411 | naics == 333412) & year <= 2011
replace naics_asm = "33392M" if floor(naics/10) == 33392 & year <= 2011
replace naics_asm = "33399N" if (naics == 333992 | naics == 333995 ///
								| naics == 333996 | naics == 333997) & year <= 2011
replace naics_asm = "33399P" if naics == 333999 & year <= 2011
replace naics_asm = "33512M" if floor(naics/10) == 33512 & year <= 2011
replace naics_asm = "33521M" if floor(naics/10) == 33521 & year <= 2011
replace naics_asm = "33592M" if floor(naics/10) == 33592  & year <= 2011
replace naics_asm = "33593M" if floor(naics/10) == 33593 & year <= 2011
replace naics_asm = "33631M" if floor(naics/10) == 33631 & year <= 2011
replace naics_asm = "33632M" if floor(naics/10) == 33632 & year <= 2011

replace naics_asm = "33712N" if naics == 337124 & year <= 2011
replace naics_asm = "33712N" if naics == 337125 & year <= 2011

replace naics_asm = "33721N" if naics == 337211 & year <= 2011
replace naics_asm = "33721N" if naics == 337214 & year <= 2011


replace naics_asm = "33991M" if floor(naics/10) == 33991 & year <= 2011
replace naics_asm = "33993M" if floor(naics/10) == 33993 & year <= 2011
replace naics_asm = "33994M" if floor(naics/10) == 33994 & year <= 2011



* all these industries == the 1 industry from ASM, time not consistent
replace naics_asm = "31121M" if floor(naics/10) == 31121 & year <= 2011

* these 2 industries == the 1 industry from ASM
replace naics_asm = "311224" if (naics == 311222 | naics == 311223) & year >= 2012
replace naics_asm = "31122N" if (naics == 311222 | naics == 311223) & year <= 2011 

replace naics_asm = "311351" if naics == 311320 & year >= 2012 

* not really match
replace naics_asm = "31171M" if naics == 311711 & year <= 2011
replace naics_asm = "311710" if naics == 311711 & year >= 2012

* ???
replace naics_asm = "311824" if (naics == 311822 | naics == 311823) & year >= 2012
replace naics_asm = "31182M" if (naics == 311822 | naics == 311823) & year <= 2011

* use NAICS 5 digit expenditure, not really match but almost! only missing 315225
replace naics_asm = "315220" if floor(naics/10) == 31522 & year >= 2012
replace naics_asm = "31522M" if floor(naics/10) == 31522 & year <= 2011

* use NAICS 5 digit expenditure, not really match. 
* 315240 after 2012 is good. 31523M before 2011 is good
* 31529M before 2011 is problematic !!!
replace naics_asm = "315240" if floor(naics/10) == 31523 & year >= 2012
replace naics_asm = "31523M" if floor(naics/10) == 31523 & year <= 2011
replace naics_asm = "315240" if naics == 315291 & year >= 2012
replace naics_asm = "31529M" if naics == 315291 & year <= 2011

* use 5 digit. do not match, missing 315291 & 315299
replace naics_asm = "315280" if naics == 315292 & year >= 2012
replace naics_asm = "31529M" if naics == 315292 & year <= 2011

* use 5 digit, do not match, missing 325132
replace naics_asm = "325130" if naics == 325131 & year >= 2012
replace naics_asm = "32513M" if naics == 325131 & year <= 2011

* use 5 digit, do not match,  missing 325182
replace naics_asm = "325180" if (naics == 325181 | naics == 325188) & year >= 2012

* use 5 digit, do not match, missing 325199 / 325191
replace naics_asm = "325194" if naics == 325192 & year >= 2012
replace naics_asm = "32519M" if naics == 325192 & year <= 2011

* use 5 digit, match
replace naics_asm = "325220" if (naics == 325221 | naics == 325222) & year >= 2012
replace naics_asm = "32522M" if (naics == 325221 | naics == 325222) & year <= 2011

* use 5 digit, not ocnsistent, not match, missing 327111
replace naics_asm = "327110" if (naics == 327112 | naics == 327113) & year >= 2012

* use 5 digit, not ocnsistent, not match, missing 327124 before 2011, missing all others after 2012
replace naics_asm = "327120" if naics == 327125 & year >= 2012
replace naics_asm = "32712N" if naics == 327125 & year <= 2011


* use 5 digit, match
replace naics_asm = "331110" if (naics == 331111 | naics == 331112) & year >= 2012
replace naics_asm = "33111M" if (naics == 331111 | naics == 331112) & year <= 2011


* use 5 digit, match, not consistent
replace naics_asm = "331313" if (naics == 331311 | naics == 331312) & year >= 2012

* use 5 digit, match, not consistent
replace naics_asm = "331410" if (naics == 331411 | naics == 331419) & year >= 2012


* use 5 digit, not match, not consistent
replace naics_asm = "31141M" if naics == 311411 & year <= 2011

* use 5 digit, match, not consistent
replace naics_asm = "31142M" if naics == 311421 & year <= 2011
replace naics_asm = "31142M" if naics == 311422 & year <= 2011
replace naics_asm = "31142M" if naics == 311423 & year <= 2011

* use 5 digit, match, not consistent
replace naics_asm = "31151N" if naics == 311511 & year <= 2011
replace naics_asm = "31151N" if naics == 311512 & year <= 2011

* use 5 digit, not match (612), not consistent
replace naics_asm = "31161N" if naics == 311611 & year <= 2011
replace naics_asm = "31161N" if naics == 311613 & year <= 2011

* use 5 digit, match, not consistent
replace naics_asm = "31191M" if naics == 311911 & year <= 2011
replace naics_asm = "31191M" if naics == 311919 & year <= 2011

* use 5 digit, match, not consistent
replace naics_asm = "31194M" if naics == 311941 & year <= 2011
replace naics_asm = "31194M" if naics == 311942 & year <= 2011

* use 5 digit, not match, not consistent
replace naics_asm = "31199M" if naics == 311999 & year <= 2011

* use 5 digit, match, not consistent
replace naics_asm = "32212M" if (naics == 322121 | naics == 322122) & year <= 2011

* use 5 digit, not match, no 325194, not consistent
replace naics_asm = "32519M" if (naics == 325193 | naics == 325199) & year <= 2011

* use 5 digit, not match, no 331222, not consistent
replace naics_asm = "33122M" if naics == 331221 & year <= 2011

* use 5 digit, not match, no 331491, not consistent
replace naics_asm = "33149M" if naics == 331492 & year <= 2011

* use 5 digit, not match, no 331512 / 331513, not consistent
replace naics_asm = "33151M" if naics == 331511 & year <= 2011



 
